package com.leapcloud.crm.core.generate;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


/**
 * 自动生成基本代码
 */
public class GenerateCode extends GenerateBase {

  /**
   * 循环生成文件
   *
   * @throws IOException
   */
  public static void main(String[] args) throws IOException, SQLException {
//    initDAO();
//    initSystem();
//    initSale();
//    initDAO("operation");
//    initOperation();
//    initCommon();
    initVO("assist", "vtiger");
  }


  public static void initSale() {
    initModule("sale", "sale");
  }

  public static void initSystem() {
    initModule("system", "sys");
  }

  public static void initOperation() {
    initModule("operation","operation");
  }

  public static void initCommon() {
    initModule("common","common");
  }

  private static void initDAO(String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      List colunmList = base.getTableColumnsForVO(tableName, tableComment);
      base.dao(tableName, tableComment, colunmList);
    }
  }

  private static void initDAO() {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables("");
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      List colunmList = base.getTableColumnsForVO(tableName, tableComment);
      base.dao(tableName, tableComment, colunmList);
    }
  }

  public static void initModule(String module, String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      String className = changeClassName(tableName, true);
      String classNameSmall = changeClassName(tableName, false);
      List colunmList = base.getTableColumnsForVO(tableName, tableComment);

      List colunmListDTO = base.getTableColumnsForDTO(tableName, tableComment);
      base.vo(module, tableName, tableComment, colunmList);
      base.dto(module, tableName, tableComment, colunmListDTO);
      base.searchVO(module, tableName, tableComment, colunmList);
      base.service(module, tableName, tableComment);
      base.serviceImpl(module, tableName, tableComment);
      base.biz(module, tableName, tableComment);
      base.bizImpl(module, tableName, tableComment,colunmList);
      base.controller(module, tableName, tableComment);
    }
  }

  public static void initVO(String module, String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      String className = changeClassName(tableName, true);
      String classNameSmall = changeClassName(tableName, false);
      List colunmList = base.getTableColumnsForVO(tableName, tableComment);
      base.vo(module, tableName, tableComment, colunmList);
    }
  }

  public static void initSearchVO(String module, String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      String className = changeClassName(tableName, true);
      String classNameSmall = changeClassName(tableName, false);
      List colunmList = base.getTableColumnsForVO(tableName, tableComment);
      base.vo(module, tableName, tableComment, colunmList);
    }
  }

  public static void initDTO(String module, String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      String className = changeClassName(tableName, true);
      String classNameSmall = changeClassName(tableName, false);
      List colunmList = base.getTableColumnsForDTO(tableName, tableComment);
      base.dto(module, tableName, tableComment, colunmList);
    }
  }

  public static void initService(String module, String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      String className = changeClassName(tableName, true);
      String classNameSmall = changeClassName(tableName, false);
      base.service(module, tableName, tableComment);
      base.serviceImpl(module, tableName, tableComment);
    }
  }

  public static void initBiz(String module, String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      String className = changeClassName(tableName, true);
      String classNameSmall = changeClassName(tableName, false);
      List colunmList = base.getTableColumnsForVO(tableName, tableComment);
      base.biz(module, tableName, tableComment);
      base.bizImpl(module, tableName, tableComment,colunmList);
    }
  }

  public static void initController(String module, String tablePrefix) {
    GenerateCode base = new GenerateCode();
    List<Map<String, Object>> list = base.getTables(tablePrefix);
    for (Map<String, Object> stringObjectMap : list) {
      String tableName = stringObjectMap.get("table_name").toString();
      String tableComment = stringObjectMap.get("table_comment").toString();
      String className = changeClassName(tableName, true);
      String classNameSmall = changeClassName(tableName, false);
      base.controller(module, tableName, tableComment);
    }
  }


}